package com.banciyuan.bcywebview.utils.http;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by hfxt2008 on 14-8-22.
 * 继承自volley的StringRequest，自定义的request，主要是为了能传加密后的map进去，具体请研究volley
 */
public class StringRequestParam extends StringRequest {

    public static String version_name;

    private String url;

    private Map<String, String> map = new HashMap<>();

    public StringRequestParam(String url, Listener<String> listener,
                              ErrorListener errorListener) {
        super(url, listener, errorListener);
        this.url = url;
    }

    public StringRequestParam(int method, String url,
                              Listener<String> listener, ErrorListener errorListener) {
        super(method, url, listener, errorListener);
        this.url = url;
    }

    public StringRequestParam(int method, String url, Map<String, String> map,
                              Listener<String> listener, ErrorListener errorListener) {
        super(method, url, listener, errorListener);
        this.map = map;
        this.url = url;
    }

    @Override
    public String getUrl() {
        return super.getUrl();
    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        return map;
    }

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        return super.parseNetworkResponse(response);
    }

    @Override
    protected VolleyError parseNetworkError(VolleyError volleyError) {
        volleyError.printStackTrace();
        return super.parseNetworkError(volleyError);
    }


    @Override
    public String getCacheKey() {
        String temp = super.getCacheKey();
        if (null != map) {
            for (Map.Entry<String, String> entry : map.entrySet())
                temp += entry.getKey() + "=" + entry.getValue();
        }
        return temp;
    }


    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> params = new HashMap<>();
        params.put("X-BCY-Version", "Android-" + version_name);
        return params;
    }

    @Override
    public Request<?> setRetryPolicy(RetryPolicy retryPolicy) {
        if (retryPolicy.getCurrentTimeout() > 10000) {
            return super.setRetryPolicy(retryPolicy);
        } else {
            DefaultRetryPolicy defaultRetryPolicy = new DefaultRetryPolicy(5000, 0,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
            return super.setRetryPolicy(defaultRetryPolicy);
        }
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        return super.getBody();
    }
}
